import requests
from bs4 import BeautifulSoup
import re

# 目标网页的 URL
url = "https://you.ctrip.com/sight/beijing1/2045497.html"

try:
    # 发送 HTTP 请求获取网页内容
    response = requests.get(url)
    # 检查请求是否成功
    response.raise_for_status()
    # 设置响应的编码
    response.encoding = response.apparent_encoding
    # 获取网页的 HTML 内容
    html_content = response.text
except requests.RequestException as e:
    print(f"请求出错: {e}")
else:
    # 创建 BeautifulSoup 对象来解析 HTML
    soup = BeautifulSoup(html_content, 'html.parser')

    # 景区官方电话
    try:
        # 使用 CSS 选择器查找电话元素
        phone_element = soup.select_one('#__next > div.poiDetailPageWrap > div > div.baseInfoModule > div.baseInfoMain > div.baseInfoContent > div.baseInfoItem.baseInfoItemPhone > div > div > span')
        if phone_element:
            phone_text = phone_element.text
            # 使用正则表达式提取电话号码
            match = re.search(r'(\d{3}-\d{8})', phone_text)
            if match:
                phone = match.group(1)
            else:
                phone = ""
        else:
            phone = ""
    except Exception as e:
        print(f"解析电话信息出错: {e}")
        phone = ""

    print(f"景区官方电话: {phone}")